package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.ZiXunFeiLeiAdapter;
import com.tangkun.environment.entity.xinwen.TypeReturn;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.CustomListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 企业类型
 */

public class QiYeLeiXingActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;

    /**
     * 选中的ID
     */
    private String selectedIds = "";
    /**
     * 数据源 企业类型
     */
    private List<TypeReturn.DataBean> dataList = new ArrayList<TypeReturn.DataBean>();
    /**
     * 适配器
     */
    private ZiXunFeiLeiAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_customlistview);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mContext = this;
        dataList = (List<TypeReturn.DataBean>) getIntent().getSerializableExtra("data");
        selectedIds = getIntent().getStringExtra("selectedIds");
        title.setText("企业类型");

        //历史选中项
        for (TypeReturn.DataBean bean : dataList) {
            if (bean.numbers.equals(selectedIds)) {
                bean.isSelected = true;
            }
        }

        adapter = new ZiXunFeiLeiAdapter(QiYeLeiXingActivity.this, dataList);
        customListView.setAdapter(adapter);
        customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                for (TypeReturn.DataBean bean : dataList) {
                    bean.isSelected = false;
                }
                dataList.get(position - 1).isSelected = true;
                //选中的ID和名字
                String ids = dataList.get(position - 1).numbers;
                String name = dataList.get(position - 1).val;
                //修改成功后调用下面代码
                Intent intent = new Intent();
                intent.putExtra("name", name);
                intent.putExtra("id", ids);
                setResult(RESULT_OK, intent);
                Utils.hideSoftKeyboard(QiYeLeiXingActivity.this, back);
                JumpHelper.finish(mContext);
            }
        });
    }


    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回按钮
                JumpHelper.finish(mContext);
                break;
        }
    }
}
